;;; file: links.lisp
;;;
;;; Copyright (c) 2010 Steffen Schulz (pepe@virtuallysecure.org)
;;; Copyright (c) 2007-2009 Cyrus Harmon (ch-lisp@bobobeach.com)
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;;   * Redistributions of source code must retain the above copyright
;;;     notice, this list of conditions and the following disclaimer.
;;;
;;;   * Redistributions in binary form must reproduce the above
;;;     copyright notice, this list of conditions and the following
;;;     disclaimer in the documentation and/or other materials
;;;     provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;

(in-package :parseltongue)

(defun get-protocol ()
  (if (ssl-p) "https" "http"))

(defun blog-entry-url (blog entry)
  (concatenate-url (blog-root-uri blog)
                   (blog-display-entry-uri blog)
                   "?id="
                   (url-encode
                     (princ-to-string (blog-entry-number entry)))))

(defun blog-archive-url (blog &key tag rss mode offset)
  (with-output-to-string (url)
    (format url "~A" (blog-root-uri blog))
    (if rss
      (format url "~A" "rss")
      (format url "~A" "archive"))
    (when tag
      (format url "?tag=~A" tag))
    (when mode
      (if tag
        (format url "&mode=~A" mode)
        (format url "?mode=~A" mode)))
    (when offset
      (if (or tag rss mode)
        (format url "&offset=~A" offset)
        (format url "?offset=~A" offset)))))

(defun blog-login-url (blog)
  (format nil "https://~A~@[:~A~]~A~A"
          (host)
          (blog-ssl-port blog)
          (blog-root-uri blog)
          (blog-login-uri blog)))

(defun blog-absolute-root-url (blog)
  (format nil "~A://~A~A"
          (get-protocol)
          (host)
          (blog-root-uri blog)))

(defun blog-absolute-entry-url (blog entry)
  (format nil "~A://~A~A"
          (get-protocol)
          (host)
          (blog-entry-url blog entry)))

(defun blog-logout-url (blog)
  (concatenate-url (blog-root-uri blog) (blog-logout-uri blog)))

(defun blog-display-entry-url (blog)
  (concatenate-url (blog-root-uri blog) (blog-display-entry-uri blog)))

(defun blog-rss-url (blog)
  (concatenate-url (blog-root-uri blog) (blog-feed-uri blog)))

(defun blog-edit-entry-url (blog entry)
  (concatenate-url (blog-root-uri blog)
                   (blog-edit-entry-uri blog)
                   "?id="
                   (princ-to-string (blog-entry-number entry))))

(defun blog-new-entry-url (blog)
  (concatenate-url (blog-root-uri blog) (blog-new-entry-uri blog)))

(defun blog-delete-entry-url (blog entry)
  (concatenate-url (blog-root-uri blog)
                   (blog-delete-entry-uri blog)
                   "?id="
                   (princ-to-string (blog-entry-number entry))))

